package com.dituo.esac.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dituo.core.exception.DtServerException;
import com.dituo.core.exception.UnauthorizedException;
import com.dituo.core.po.UserBasePO;
import com.dituo.core.response.DituoResponseDTO;
import com.dituo.core.response.PageResultDTO;
import com.dituo.core.threadlocal.UserBaseThreadLocal;
import com.dituo.core.util.DituoStringUtil;
import com.dituo.esac.dto.EsacAppInfoDTO;
import com.dituo.esac.dto.EsacCheckSaveDTO;
import com.dituo.esac.dto.EsacExportDTO;
import com.dituo.esac.dto.EsacImportDTO;
import com.dituo.esac.dto.EsacSearchDTO;
import com.dituo.esac.po.EsacCheckInfoPO;
import com.dituo.esac.po.EsacCheckPO;
import com.dituo.esac.po.EsacDeviceNodePO;
import com.dituo.esac.po.EsacInfoPO;
import com.dituo.esac.po.EsacNodeListPO;
import com.dituo.esac.po.EsacTemListPO;
import com.dituo.esac.service.EsacService;
import com.dituo.esac.util.EsacParamUtils;

import reactor.core.publisher.Mono;

/**
 * 消防物资控制器类
 * @author wanghao
 * @date 2019-11-14 10:22
 */
@RestController
@RequestMapping("/esac")
public class EsacController {
    
	private static Logger logger = LoggerFactory.getLogger(EsacController.class);
	
	@Autowired
	EsacService esacService;
	
	/**
	 * 查询人员历史数据
	 * @return DituoResponseDTO
	 * @throws UnauthorizedException
	 */
	@GetMapping("/list")
	public DituoResponseDTO listEsacHistoryPage(Integer pageNum, Integer pageSize, Short buildingNumber,
			Short room, String personName, String idCard, String communityName,
			Short gender, Short checkStatus) throws UnauthorizedException {
		
		UserBasePO userBase = UserBaseThreadLocal.getUserBase();
		EsacSearchDTO esacSearchDTO = new EsacSearchDTO();
		EsacParamUtils.setUserBaseParam(esacSearchDTO, userBase);
		EsacParamUtils.setPageParam(esacSearchDTO, pageNum, pageSize);
		esacSearchDTO.setBuildingNumber(buildingNumber);
		esacSearchDTO.setRoom(room);
		if (DituoStringUtil.notEmpty(personName)) {
			esacSearchDTO.setPersonName(personName);
		}
		if (DituoStringUtil.notEmpty(idCard)) {
			esacSearchDTO.setIdCard(idCard);
		}
		if (DituoStringUtil.notEmpty(communityName)) {
			esacSearchDTO.setCommunityName(communityName);
		}
		esacSearchDTO.setGender(gender);
		esacSearchDTO.setCheckStatus(checkStatus);
		logger.info("RequestParam {}", esacSearchDTO);
		PageResultDTO<List<EsacNodeListPO>> nodeListPage = esacService.listEsacHistoryPage(esacSearchDTO);
		return new DituoResponseDTO(HttpStatus.OK, nodeListPage);
	}
	
	/**
	 * 查询人员历史数据
	 * @return DituoResponseDTO
	 * @throws UnauthorizedException
	 */
	@GetMapping("/list_tem")
	public DituoResponseDTO listEsacTemPage(Integer pageNum, Integer pageSize, Short buildingNumber,
			Short room, String personName, String idCard, String communityName,
			Short gender, Short checkStatus) throws UnauthorizedException {
		
		UserBasePO userBase = UserBaseThreadLocal.getUserBase();
		EsacSearchDTO esacSearchDTO = new EsacSearchDTO();
		EsacParamUtils.setUserBaseParam(esacSearchDTO, userBase);
		EsacParamUtils.setPageParam(esacSearchDTO, pageNum, pageSize);
		esacSearchDTO.setBuildingNumber(buildingNumber);
		esacSearchDTO.setRoom(room);
		if (DituoStringUtil.notEmpty(personName)) {
			esacSearchDTO.setPersonName(personName);
		}
		if (DituoStringUtil.notEmpty(idCard)) {
			esacSearchDTO.setIdCard(idCard);
		}
		if (DituoStringUtil.notEmpty(communityName)) {
			esacSearchDTO.setCommunityName(communityName);
		}
		esacSearchDTO.setGender(gender);
		esacSearchDTO.setCheckStatus(checkStatus);
		logger.info("RequestParam {}", esacSearchDTO);
		PageResultDTO<List<EsacTemListPO>> nodeListPage = esacService.listEsacTemPage(esacSearchDTO);
		return new DituoResponseDTO(HttpStatus.OK, nodeListPage);
	}
	
	/**
	 * 详情
	 */
	@GetMapping("/info")
	public DituoResponseDTO getEsacInfo(String personId) throws UnauthorizedException {
		EsacInfoPO esacInfoPO = esacService.getEsacInfo(personId);
		return new DituoResponseDTO(HttpStatus.OK, esacInfoPO);
	}
	
	/**
	 * app详情
	 */
	@GetMapping("/info_app")
	public DituoResponseDTO getEsacAppInfo(Short buildingNumber, Short room, String communityName) throws UnauthorizedException {
		EsacSearchDTO esacSearchDTO = new EsacSearchDTO();
		esacSearchDTO.setBuildingNumber(buildingNumber);
		esacSearchDTO.setRoom(room);
		if (DituoStringUtil.notEmpty(communityName)) {
			esacSearchDTO.setCommunityName(communityName);
		}
		EsacAppInfoDTO appInfoPO = esacService.getAppInfo(esacSearchDTO);
		return new DituoResponseDTO(HttpStatus.OK, appInfoPO);
	}
	
	/**
	 * app历史 温度
	 * @return DituoResponseDTO
	 * @throws UnauthorizedException
	 */
	@GetMapping("/info_app_check")
	public DituoResponseDTO listEsacAppCheckPage(Integer pageNum, Integer pageSize, String personId) throws UnauthorizedException {
		
		EsacSearchDTO esacSearchDTO = new EsacSearchDTO();
		EsacParamUtils.setPageParam(esacSearchDTO, pageNum, pageSize);
		if (DituoStringUtil.notEmpty(personId)) {
			esacSearchDTO.setPersonId(personId);
		}
		logger.info("RequestParam {}", esacSearchDTO);
		PageResultDTO<List<EsacCheckPO>> nodeListPage = esacService.listEsacCheckPage(esacSearchDTO);
		return new DituoResponseDTO(HttpStatus.OK, nodeListPage);
	}
	
	/**
	 * 温度历史
	 * @return DituoResponseDTO
	 * @throws UnauthorizedException
	 */
	@GetMapping("/info_check")
	public DituoResponseDTO listEsacCheckPage(Integer pageNum, Integer pageSize, String personId) throws UnauthorizedException {
		
		EsacSearchDTO esacSearchDTO = new EsacSearchDTO();
		EsacParamUtils.setPageParam(esacSearchDTO, pageNum, pageSize);
		if (DituoStringUtil.notEmpty(personId)) {
			esacSearchDTO.setPersonId(personId);
		}
		logger.info("RequestParam {}", esacSearchDTO);
		PageResultDTO<List<EsacCheckPO>> nodeListPage = esacService.listEsacCheckPage(esacSearchDTO);
		return new DituoResponseDTO(HttpStatus.OK, nodeListPage);
	}
	
	/**
	 * 人员温度登记
	 * 
	 * @param esacCheckSaveDTO
	 * @return
	 * @throws UnauthorizedException
	 * @throws DtServerException
	 */
	@PostMapping("/save_check")
	public DituoResponseDTO saveEsacCheck(@RequestBody EsacCheckSaveDTO esacCheckSaveDTO) throws DtServerException {
		
		String result = esacService.saveEsacCheck(esacCheckSaveDTO);
		
		return new DituoResponseDTO(HttpStatus.OK, result);
	}
	
	/**
	 * 社区人员信息导入
	 * @param esacImportDTO
	 * @return
	 * @throws UnauthorizedException
	 * @throws DtServerException
	 */
	@PostMapping(path = "/import", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
	public DituoResponseDTO importEsacInfo(EsacImportDTO esacImportDTO) throws UnauthorizedException, DtServerException {
		
		return new DituoResponseDTO(HttpStatus.OK, esacService.importEsacInfo(esacImportDTO));
	}
	
	/**
	 * 查询疫情防控小区信息
	 * @param time
	 * @param orgid
	 * @return
	 * @throws UnauthorizedException
	 */
	@GetMapping("/info_community")
	public DituoResponseDTO getEsacCommunity(Long orgid) throws UnauthorizedException {
		if (null == orgid) {
			UserBasePO userBase = UserBaseThreadLocal.getUserBase();
			orgid = userBase.getOrgId();
		}
		return new DituoResponseDTO(HttpStatus.OK, esacService.getEsacCommunity(orgid));
	}
	
	/**
	 * 根据日期查询人员状态信息
	 * @param time
	 * @param orgid
	 * @return
	 * @throws UnauthorizedException
	 */
	@GetMapping("/day_person")
	public DituoResponseDTO getEsacDayPerson(String time, Long orgid) throws UnauthorizedException {
		if (null == orgid) {
			UserBasePO userBase = UserBaseThreadLocal.getUserBase();
			orgid = userBase.getOrgId();
		}
		return new DituoResponseDTO(HttpStatus.OK, esacService.getEsacDayPerson(time,orgid));
	}
	
	/**
	 * 近7天按天统计门开告警的数量
	 * 
	 * @return
	 * @throws UnauthorizedException
	 */
	@GetMapping("/warn_count_day")
	public DituoResponseDTO getEsacWarnCountDay() throws UnauthorizedException {
		UserBasePO userBase = UserBaseThreadLocal.getUserBase();
		Long orgid = userBase.getOrgId();
		return new DituoResponseDTO(HttpStatus.OK, esacService.getEsacWarnCountDay(orgid, userBase.getRoleAreaCode(), userBase.getOrgType()));
	}
	
	/**
	 * 体温检测异常
	 * @return DituoResponseDTO
	 * @throws UnauthorizedException
	 */
	@GetMapping("/list_status")
	public DituoResponseDTO listEsacTemperaturePage(Integer pageNum, Integer pageSize) throws UnauthorizedException {
		
		UserBasePO userBase = UserBaseThreadLocal.getUserBase();
		EsacSearchDTO esacSearchDTO = new EsacSearchDTO();
		EsacParamUtils.setUserBaseParam(esacSearchDTO, userBase);
		EsacParamUtils.setPageParam(esacSearchDTO, pageNum, pageSize);
		PageResultDTO<List<EsacCheckInfoPO>> nodeListPage = esacService.listEsacTemStatusPage(esacSearchDTO);
		return new DituoResponseDTO(HttpStatus.OK, nodeListPage);
	}
	
	/**
	 * 近7天按天统计体温
	 * 
	 * @return
	 * @throws UnauthorizedException
	 */
	@GetMapping("/tem_count_day")
	public DituoResponseDTO getEsacTemWarnCountDay() throws UnauthorizedException {
		UserBasePO userBase = UserBaseThreadLocal.getUserBase();
		return new DituoResponseDTO(HttpStatus.OK, esacService.getEsacTempetWarnCountDay(userBase.getOrgId()));
	}

	/**
	 * 节点控制分页查询
	 * 
	 * @param pageSize 每页显示数
	 * @param pageNum 页面
	 * @param eventType 事件状态
	 * @param nameOrEuid 地址和设备号查询条件
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @param deviceStatus 设备状态 1-在线 2-离线
	 * @return DituoResponseDTO
	 * @throws UnauthorizedException
	 */
	@GetMapping("/list_device")
	public DituoResponseDTO listFiredoorNodePage(Integer pageNum, Integer pageSize, Integer eventType, 
			String nameOrEuid, String startTime, String endTime, Integer deviceStatus, Integer sort, Integer type, Long searchOrgId) throws UnauthorizedException {
		
		UserBasePO userBase = UserBaseThreadLocal.getUserBase();
		if (null != searchOrgId) {
			userBase.setOrgId(searchOrgId);
		}
		EsacExportDTO esacExportDTO = EsacParamUtils.setFiredoorNodeSearchParam(pageNum, pageSize, 
				eventType, nameOrEuid, startTime, endTime, deviceStatus, userBase);
		esacExportDTO.setSort(sort);
		esacExportDTO.setType(type);
		logger.info("RequestParam {}", esacExportDTO);

		PageResultDTO<List<EsacDeviceNodePO>> nodeListPage = esacService.listEsacDeviceNodePage(esacExportDTO);
		return new DituoResponseDTO(HttpStatus.OK, nodeListPage);
	}
	
	/**
	 * 查询设备历史数据
	 * 
	 * @param pageSize 每页显示数
	 * @param pageNum 页面
	 * @param eventType 事件状态 
	 * @param nameOrEuid 地址和设备号查询条件
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @param alert
	 * @return DituoResponseDTO
	 * @throws UnauthorizedException
	 */
	@GetMapping("/list_device_history")
	public DituoResponseDTO listFiredoorHistoryPage(Integer pageNum, Integer pageSize, Integer eventType, 
			String nameOrEuid, String startTime, String endTime, Integer type, Integer deviceStatus, Integer sort, Long searchOrgId) throws UnauthorizedException {
		
		UserBasePO userBase = UserBaseThreadLocal.getUserBase();
		if (null != searchOrgId) {
			userBase.setOrgId(searchOrgId);
		}
		EsacExportDTO esacExportDTO = EsacParamUtils.setFiredoorNodeSearchParam(pageNum, pageSize, 
				eventType, nameOrEuid, startTime, endTime, deviceStatus, userBase);
		esacExportDTO.setType(type);
		esacExportDTO.setSort(sort);
		
		logger.info("RequestParam {}", esacExportDTO);
		
		PageResultDTO<List<EsacDeviceNodePO>> nodeListPage = esacService.listEsacDeviceHistoryPage(esacExportDTO);
		return new DituoResponseDTO(HttpStatus.OK, nodeListPage);
	}
	
	/**
	 * 数据导出excel
	 * 
	 * @param response
	 * @param nameOrEuid 地址和设备号查询条件
	 * @param eventType 事件状态 
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @param type 1-节点控制 2-报警记录 3-历史记录
	 * @return
	 * @throws Exception 
	 */
	@GetMapping("/export_excel")
	public Mono<Void> exportExcel(ServerHttpResponse response, String nameOrEuid, Integer eventType, 
			 String startTime, String endTime, Integer type, Long searchOrgId) throws UnauthorizedException {
	    try {
	    	UserBasePO userBase = UserBaseThreadLocal.getUserBase();
	    	if (null != searchOrgId) {
				userBase.setOrgId(searchOrgId);
			}
	    	EsacExportDTO esacExportDTO = new EsacExportDTO();
	    	esacExportDTO.setNameOrEuid(nameOrEuid);
	    	esacExportDTO.setEventType(eventType);
	    	esacExportDTO.setType(type);
	    	EsacParamUtils.setDeviceDateRangeParam(esacExportDTO, startTime, endTime);
	    	EsacParamUtils.setDeviceUserBaseParam(esacExportDTO, userBase);
	    	
			return esacService.exportExcel(response, esacExportDTO);
		} catch (Exception e) {
			return null; 
		}
		
	}
}
